{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4a562cb-370f-45f5-b01c-8d2656685b17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Speed Test -- Trajectory Plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e636c33-597a-4e42-a082-9636b2d0f960",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from geopandas import GeoDataFrame, read_file\n",
    "from shapely.geometry import Point, LineString, Polygon\n",
    "from datetime import datetime, timedelta\n",
    "import matplotlib.pyplot as plt\n",
    "import movingpandas as mpd\n",
    "from holoviews import opts, dim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ed48d97-5225-4bc9-a169-d67b38393582",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = read_file('../data/geolife_small.gpkg')\n",
    "runtimes={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edc4150b-a764-4e14-9b2c-fa5e90a9a933",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "t0 = datetime.now()\n",
    "gdf.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d97091e-0bc6-4028-9e24-19f558180162",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['GeoDataFrame.plot'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "687d82b8-3c9e-4efb-a0a9-2d0e5498e896",
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e442404-3c0f-48a6-801b-78e3a8ead115",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "t0 = datetime.now()\n",
    "traj_collection.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2403a91a-f46b-4321-a97c-4ab085dfc30a",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.plot'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b736bfa8-49c6-42ae-a98c-6596257f999a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "t0 = datetime.now()\n",
    "traj_collection.hvplot(line_width=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ade6d8a2-1a63-487e-b267-63b590e3ecd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.hvplot'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa2990fd-b225-44aa-a455-3bb25759e034",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "t0 = datetime.now()\n",
    "traj_collection.hvplot(line_width=7, frame_width=300, frame_height=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c46940b7-ec00-4505-827f-f46c33c7d56d",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.hvplot (smaller)'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd831443-72fb-4a5c-b57b-a4f66d68cf95",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "generalized_traj = mpd.DouglasPeuckerGeneralizer(traj_collection).generalize(tolerance=0.01)\n",
    "t0 = datetime.now()\n",
    "generalized_traj.hvplot(line_width=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bb8f744-6296-4da3-94da-f0a9c0b5b7e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.hvplot (generalized)'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acee0410-1b74-4b78-9dae-1b16333770e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "t0 = datetime.now()\n",
    "gdf.hvplot(geo=True, tiles='OSM')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23062196-d172-4284-952d-5cbc475bcf4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['GeoDataFrame.hvplot'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39b7267f-dced-4e0c-974a-9c6e8057b08d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "line_gdf = traj_collection.to_line_gdf()\n",
    "t0 = datetime.now()\n",
    "line_gdf.hvplot(geo=True, tiles='OSM', line_width=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6967ee3-0b71-4b0d-8692-28ddf9ff0077",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.to_line_gdf.hvplot'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "849ace0c-87be-46d4-9ae0-2ef349e02878",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "line_gdf = traj_collection.to_line_gdf()\n",
    "t0 = datetime.now()\n",
    "line_gdf.hvplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "077fff01-117d-40d1-bc98-6f1ab54a8c0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.to_line_gdf.hvplot (no basemap)'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45b6812b-a994-4770-a288-a1acdb9215a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "traj_gdf = traj_collection.to_traj_gdf()\n",
    "t0 = datetime.now()\n",
    "traj_gdf.hvplot(geo=True, tiles='OSM', line_width=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10211529-c0e8-4ef9-9bc7-49e7e3e97237",
   "metadata": {},
   "outputs": [],
   "source": [
    "runtime = datetime.now()-t0\n",
    "runtimes['TrajectoryCollection.to_traj_gdf.hvplot'] = runtime\n",
    "print(runtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0d9f7eb-908f-498c-af62-3645b13273e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "for key, value in sorted(runtimes.items()):\n",
    "    print(f'{key}: {value}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8076c72-0f87-4d4c-a7c6-0ff82d19a17c",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pd.DataFrame.from_dict(runtimes, orient='index', columns=['runtime'])\n",
    "result['seconds'] = result.runtime.dt.total_seconds()\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63afedc4-5038-4cb8-852b-d8abe2f8665c",
   "metadata": {},
   "outputs": [],
   "source": [
    "result.sort_values('seconds').hvplot.barh(y='seconds', title='Runtimes in seconds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73c7844f-72c0-45f0-8a51-c09e708f8a2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas\n",
    "print(f'GeoPandas {geopandas.__version__}')\n",
    "import geoviews\n",
    "print(f'Geoviews {geoviews.__version__}')\n",
    "import cartopy\n",
    "print(f'Cartopy {cartopy.__version__}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8cfe677-e71b-474b-9a75-6212929f6d97",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
